package com.sdu.cloudservice.dao;

import com.sdu.cloudservice.entity.ActualPriceEntity;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * Create by zekdot on 19-6-22.
 * @author
 * ZMX
 */
@Mapper
@Repository
public interface ActualPriceDao {
    /**
     * 根据可用区id和实例id获取最后一条价格实体
     * @param zoneId
     * @param instanceId
     * @return
     *
     */
    @Select("SELECT * FROM actual_price WHERE zone_id=#{zoneId} AND instance_id=#{instanceId} ORDER BY time DESC LIMIT 1")
    public ActualPriceEntity getActualPriceByZoneIdAndInstanceId(@Param("zoneId") String zoneId,@Param("instanceId") String instanceId);

    @Select("SELECT * FROM actual_price WHERE zone_id=#{zoneId} AND instance_id=#{instanceId} AND time=#{time} ")//1、找出等于的
    public ActualPriceEntity getActualPriceByZoneIdAndInstanceId1(@Param("zoneId") String zoneId, @Param("instanceId") String instanceId, @Param("time")String time);

    @Select("SELECT * FROM actual_price WHERE zone_id=#{zoneId} AND instance_id=#{instanceId} AND time<#{time} ORDER BY time DESC LIMIT 1")//2、找出小于的
    public ActualPriceEntity getActualPriceByZoneIdAndInstanceId2(@Param("zoneId") String zoneId, @Param("instanceId") String instanceId, @Param("time")String time);

    @Select("SELECT * FROM actual_price WHERE zone_id=#{zoneId} AND instance_id=#{instanceId} AND time<#{end_time} AND time>#{start_time} ORDER BY time DESC")
    public List<ActualPriceEntity> getActualPriceByZoneIdAndInstanceId3(@Param("zoneId")String zoneId, @Param("instanceId") String instanceId, @Param("end_time")String end_time,@Param("start_time") String start_time);

    @Select("SELECT * FROM actual_price WHERE zone_id=#{zoneId} AND instance_id=#{instanceId} AND time<#{end_time} AND time>#{start_time} ORDER BY time ASC")
    public List<ActualPriceEntity> getActualPriceByZoneIdAndInstanceId4(@Param("zoneId")String zoneId, @Param("instanceId") String instanceId, @Param("end_time")String end_time,@Param("start_time") String start_time);
    /**
     * 插入一个价格实体
     * @param zoneId
     * @param instanceId
     * @param time
     * @param origin
     * @param spot
     * @return
     */
    @Insert("INSERT INTO actual_price VALUES(#{zoneId},#{instanceId},#{time},#{origin},#{spot})")
    public int insertIntoActualPrice(@Param("zoneId")String zoneId,@Param("instanceId")String instanceId,@Param("time")String time,@Param("origin")double origin,@Param("spot")double spot);

    @Update("UPDATE actual_price SET time=#{time} WHERE zone_id=#{zoneId} AND instance_id=#{instanceId} AND time=#{curTime}")
    public int updateTimeByZoneIdAndInsId(@Param("time")String time,@Param("zoneId")String zoneId,@Param("instanceId")String instanceId,@Param("curTime")String curTime);
}
